'use strict'
import { app, BrowserWindow, ipcMain } from 'electron'
import qiniuUpload from './qiniu'
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path')
    .join(__dirname, '/static')
    .replace(/\\/g, '\\\\')
}

let mainWindow
const winURL =
  process.env.NODE_ENV === 'development'
    ? `http://localhost:9080`
    : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000,
    webPreferences: {
      allowRunningInsecureContent: true,
      experimentalFeatures: true
    }
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

ipcMain.on('asynchronous-message', (event, arg) => {
  qiniuUpload.uploadFile(arg, arg, qiniuUpload.overrideUploadToken(arg), (err, data) => {
    event.sender.send('asynchronous-message', JSON.stringify({
      arg,
      err,
      data
    }))
  })
  // event.sender.send('asynchronous-message', qiniuUpload.overrideUploadToken(arg))
})

ipcMain.on('ping', (event, arg) => {
  event.sender.send('pong', Math.random())
})

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
ipcMain.on('file-manager', (event, args) => {
  const callback = (err, data) => {
    event.sender.send('file-manager-resp', {
      err,
      data
    })
  }
  const {
    type,
    filePath,
    key,
    keys,
    file,
    files,
    pageOption,
    lifeCircleOption
  } = args
  switch (type) {
    case 'uploadFile':
      qiniuUpload.uploadFile(filePath, key, qiniuUpload.overrideUploadToken(key), callback)
      break
    case 'fileCopy':
      qiniuUpload.fileCopy(file, callback)
      break
    case 'fileCopyBatch':
      qiniuUpload.fileCopyBatch(files, callback)
      break
    case 'fileDel':
      qiniuUpload.fileDel(key, callback)
      break
    case 'fileDelBatch':
      qiniuUpload.fileDelBatch(keys, callback)
      break
    case 'fileDelAfterDays':
      qiniuUpload.fileDelAfterDays(lifeCircleOption, callback)
      break
    case 'fileList':
      qiniuUpload.fileList(pageOption, callback)
      break
    case 'fileMove':
      qiniuUpload.fileMove(file, callback)
      break
    case 'fileMoveBatch':
      qiniuUpload.fileMoveBatch(files, callback)
      break
    case 'fileState':
      qiniuUpload.fileState(file, callback)
      break
    case 'fileStateBatch':
      qiniuUpload.fileStateBatch(files, callback)
      break
    default:
      break
  }
})
